package py.com.fms.ven.persistence.services;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import com.ibatis.sqlmap.client.SqlMapClient;

import py.com.fms.exception.CommonException;
import py.com.fms.fms.bean.SupplierBean;
import py.com.fms.persistence.services.FmsServices;
import py.com.fms.ven.bean.CustomerBean;

public class CustomerServices extends FmsServices {

	public CustomerServices(SqlMapClient client) {
		super(client);
		// TODO Auto-generated constructor stub
	}
	
	/**
	 [[[
	 [[ The following method retrieve a list of all records
	 [[ Normally used to load a grid.
	 */
	
	@SuppressWarnings("unchecked")
	public List<CustomerBean> getAllCustomers() throws CommonException{
		List<CustomerBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis ]]
			
			queryForList = (List<CustomerBean>)this.getConfig().queryForList("customer.getAllCustomers");
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	/**
	 [[[
	 [[ The following method store a bean (record) in the database.
	 */
	
	public void saveNewCustomer(CustomerBean customer) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().insert("customer.insCustomer", customer);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 [[[
	 [[ The following method delete a record from the database.
	 */
	
	public void deleteCustomerById(long id) throws CommonException{
		try {
			HashMap<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("idCustomer", id);
			this.getConfig().startTransaction();
			this.getConfig().delete("customer.delCustomerById", parameters);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 [[[
	 [[ The following method update a record from a bean.
	 */
	public void updateCustomer(CustomerBean customer) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("customer.updCustomerById", customer);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 [[[
	 [[ The following method retrieve a object containing the record id
	 */
	

	public long getIdCustomer(String cod) throws CommonException{
		long queryForObject;
		try {
			
			//[[ queryForList is an implemented interface of iBatis ]] 
			
			queryForObject = (Long)this.getConfig().queryForObject("customer.getIdCustomer", cod);
			return queryForObject;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	public CustomerBean getCustomerData(Long id) throws CommonException{
		CustomerBean queryForObject;
		try {
			
			//[[ queryForList is an implemented interface of iBatis ]] 
			
			queryForObject = (CustomerBean)this.getConfig().queryForObject("customer.getCustomerData", id);
			return queryForObject;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<CustomerBean> getCustomerPickList(String cod) throws CommonException{
		List<CustomerBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis ]] 
			HashMap< String, Object> param = new HashMap<String, Object>();
			param.put("cod", cod);
			queryForList = (List<CustomerBean>)this.getConfig().queryForList("customer.getCustomerPickList", param);
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}

}
